/*
 * Copyright (C) 2010, Funcom AS, All Rights Reserved
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 *  @authors
 *  Svein Gunnar Larsen <sveingunnarlarsen@gmail.com>
 *  Erik Thune Lund <culex.eriklund@gmail.com>
 *  Michel Krohn-Dale <kronicz@gmail.com>
 */

package com.funcom.peeler.component;

import com.funcom.peeler.peelable.PeelComponent;
import com.funcom.peeler.peelable.PeelContainer;
import com.jme.renderer.ColorRGBA;
import com.jme.renderer.Renderer;
import com.jmex.bui.BComponent;
import com.jmex.bui.BContainer;
import com.jmex.bui.BImage;
import com.jmex.bui.background.BlankBackground;
import org.lwjgl.opengl.GL11;

import java.awt.*;

public class SelectionBox extends BComponent {

    private PeelComponent component;
    private Point coords;

    public SelectionBox( PeelComponent component ) {
        super( "selection_overlay" );
        this.component = component;
        this.coords = new Point( component.getComponent().getX(), component.getComponent().getY() );

        setStyleClass("selectedcomp");
        createBackground();
        setHoverEnabled(false);
    }

    private void createBackground() {

        setBackground( 0, new BlankBackground() {

            public void render( Renderer renderer, int x, int y, int width, int height, float alpha ) {
                super.render( renderer, x, y, width, height, alpha );

                ColorRGBA color = component.hasGroup() ? ColorRGBA.green : ColorRGBA.black;
                coords.x = component.getComponent().getX();
                coords.y = component.getComponent().getY();

                PeelContainer peelParent = component.getParent();
                BContainer parent = peelParent == null ? null : peelParent.getComponent();

                if ( parent == null ) return;
                
                while ( parent != null ) {
                    coords.translate( parent.getX(), parent.getY() );
                    parent = parent.getParent();
                }

                BComponent.applyDefaultStates();
                BImage.blendState.apply();

                GL11.glColor4f( color.r, color.g, color.b, alpha );
                GL11.glDisable( GL11.GL_LINE_STIPPLE );
                GL11.glLineWidth( 2 );
                GL11.glPushAttrib( GL11.GL_LINE_BIT );
                GL11.glBegin( GL11.GL_LINE_LOOP );
                GL11.glVertex2f( coords.x, coords.y );
                GL11.glVertex2f( coords.x + component.getComponent().getWidth(), coords.y );
                GL11.glVertex2f( coords.x + component.getComponent().getWidth(), coords.y + component.getComponent().getHeight() );
                GL11.glVertex2f( coords.x, coords.y + component.getComponent().getHeight() );
                GL11.glEnd();

                GL11.glColor4f( ColorRGBA.white.r, ColorRGBA.white.g, ColorRGBA.white.b, alpha );
                GL11.glEnable( GL11.GL_LINE_STIPPLE );

                GL11.glLineWidth( 2 );
                GL11.glPushAttrib( GL11.GL_LINE_BIT );
                GL11.glLineStipple( 4, (short)0xAAAA );
                GL11.glBegin( GL11.GL_LINE_LOOP );
                GL11.glVertex2f( coords.x, coords.y );
                GL11.glVertex2f( coords.x + component.getComponent().getWidth(), coords.y );
                GL11.glVertex2f( coords.x + component.getComponent().getWidth(), coords.y + component.getComponent().getHeight() );
                GL11.glVertex2f( coords.x, coords.y + component.getComponent().getHeight() );
                GL11.glEnd();
            }
        });
    }
}
